//index.js
//获取应用实例
var util = require('../../utils/util.js')
var app = getApp()
Page({
  data: {
    formType: {},
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    time: {},
    winWidth: 0,
    winHeight: 0,
    // tab切换 
    currentTab: 0,
    ontips: true,
    //note内容，从后台获取
    note: [],
    needDelete: -1,
    iscontent: true,
  },
  onLoad: function () {
    var _this = this;
    if(_this.data.needDelete != -1){
      console.log(_this.data.needDelete)
      note.splice(_this.data.needDelete,1)
    }
    wx.getSystemInfo({

      success: function (res) {
        _this.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }

    });
  },
  //授权事件
  getUserInfo_zh: function (e) {
    var _this = this
    if (e.detail.userInfo) {

      app.setUserinfo();
      _this.setData({
        hasUserInfo: false
      })
    } else {
      wx.showToast({
        title: "为了您更好的体验,请先同意授权",
        icon: 'none',
        duration: 2000
      });
    }
  },
  //创建目标事件
  createGoal: function (e) {
    var _this = this;
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userInfo']) {
          _this.setData({
            hasUserInfo: true
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.request({
      //便签的url
      url: `${app.globalData.apiurl}api/index/getNote`,
      method: 'GET',
      data: {
        uid: app.globalData.userInfo.uid
      },
      success: res => {
        console.log(res)
        //判断用户手记内容是否为空
        if (res.data.data.note.length == 0) {
          this.setData({
            iscontent: false
          })
        }
        else{
          var temp = res.data.data.note;
          for (var i = 0; i < temp.length; i++) {
            var date = temp[i].create_time;
            var format;
            //console.log(date);
            var regular = util.formatTimeTwo(date, 'Y/M/D h:m');
            temp[i].create_time = regular;
            res.data.data.note.create_time = regular;
            //转化后的日期存放在temp中
          }
         this.setData({
           iscontent: true,
           note: res.data.data.note,
         })
        }
      }
    })
  },
  createNote: function () {
    wx.navigateTo({
      url: '/pages/createNote/createNote',
    })
  },
  showContent: function(e){
    var that = this
    var id = e.currentTarget.dataset.id;
    console.log();
    wx.navigateTo({
      url: '/pages/detail/detail?create_time=' + that.data.note[id].create_time+'&text=' + that.data.note[id].text+'&id='+id+'&noteid='+that.data.note[id].id+'&uid='+that.data.uid+'&title='+that.data.note[id].title,
    })
  }
})
